﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMSGSC.DAL;
using PMSGSC.Common;
using PMSGSC.BLL;

namespace PMSGSC.App
{
    public partial class frmFilterStockItem : DevExpress.XtraEditors.XtraForm
    {
        public frmFilterStockItem(frmMain objMain)
        {
            InitializeComponent();
            stockBindingSource.DataSource = Stock_BLL.GetAll(frmMain.ship.Ship_ID);
            main = objMain;
        }
        frmMain main;
        bool isStop = true;
        List<PartsInEquipment> partList = new List<PartsInEquipment>();
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                frmProcessBar pb = new frmProcessBar();
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(() =>
                {
                    LoadSkinToForm();
                    pb.ShowDialog();
                }
                ));
                thread.Start();

                isStop = false;
                partList = Search();

                if (partList.Count > 0)
                {
                    frmStockItem stockItem = new frmStockItem(partList, main);
                    stockItem.MdiParent = main;
                    stockItem.Show();

                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    this.Close();
                }
                else
                {
                    if (pb.InvokeRequired)
                    {
                        pb.BeginInvoke(new Action(() => pb.Close()));
                        isStop = true;
                    }
                    XtraMessageBox.Show("Not result in display", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private List<PartsInEquipment> Search()
        {
            PartsInEquipment part = new PartsInEquipment();

            part.PartCode = ucLoadPartInEquipment1.txtCode.Text;
            part.PartName = ucLoadPartInEquipment1.txtName.Text;

            if (ucLoadMaker1.lb_id.Text == "")
                part.Maker_ID = 0;
            else
                part.Maker_ID = int.Parse(ucLoadMaker1.lb_id.Text);

            if (ucLoadVendor1.lbid.Text == "")
                part.Vendor_ID = 0;
            else
                part.Vendor_ID = int.Parse(ucLoadVendor1.lbid.Text);

            if (ucLoadEquipment1.lbid.Text == "")
                part.Equipment_ID = 0;
            else
                part.Equipment_ID = int.Parse(ucLoadEquipment1.lbid.Text);

            part.DrawingNo = txtDrawingNo.Text;
            int StockId = 0;
            if (gluLocation.Text != "")
                StockId = int.Parse(gluLocation.EditValue.ToString());

            return PartInEquipment_BLL.FilterStockItem(part, StockId, frmMain.ship.Ship_ID);
        }

        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Blue");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                this.Close();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
           
        }
    }
}